<?php

namespace Xin\EasyQueue\Webman;

use Throwable;
use Webman\RedisQueue\Consumer as ConsumerContact;

abstract class QueueConsumer implements ConsumerContact
{
    /**
     * 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
     * @var string
     */
    public $connection = 'default';

    /**
     * @var array
     */
    protected $data;

    /**
     * @inerhitDoc
     */
    public function consume($data)
    {
        $this->data = $data;
        $this->handle();
    }

    /**
     * @return void
     */
    abstract protected function handle();

    /**
     * 默认失败处理
     * @param Throwable $e
     * @param mixed $package
     * @return void
     */
    public function onConsumeFailure(Throwable $e, $package)
    {
        echo static::class . " consume failure:", $e->getMessage(), $e->getTraceAsString(), var_export($package, true);
    }
}
